//
//  LoveView.m
//  NewMusic
//
//  Created by dllo on 15/11/23.
//  Copyright © 2015年 Going. All rights reserved.
//

#import "LoveView.h"
#import "HisTableViewCell.h"
@interface LoveView ()<UITableViewDelegate, UITableViewDataSource, HQdeleHisCell, UIScrollViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *arr;//buttontitle
@property (nonatomic, retain) NSMutableArray *arrLocal;
@end

@implementation LoveView
- (void)dealloc
{
    [_tableView release];
    [_arrLocal release];
    [_arr release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatSubView];
    }
    return self;
}
- (void)creatSubView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 580) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addSubview:self.tableView];
    [self.tableView registerClass:[HisTableViewCell class] forCellReuseIdentifier:@"cell"];
    [_tableView release];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 130)];
    self.tableView.tableHeaderView = view;
    [view release];
    UIView *viewBG = [[UIView alloc] initWithFrame:view.frame];
    viewBG.backgroundColor = [UIColor whiteColor];
    viewBG.alpha = 0.05;
    [view addSubview:viewBG];;
    [viewBG release];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 150, 20)];
    label.text = @"搜索热词";
    label.textColor = [UIColor colorWithRed:220 / 255.0 green:220 / 255.0 blue:220 / 255.0 alpha:1.0];
    [view addSubview:label];
    [label release];
    
    self.arr = [NSMutableArray arrayWithObjects:@"小苹果", @"南山南", @"王菲", @"tyboys", @"陈奕迅", @"邓紫棋", @"儿歌", @"刘德华", @"张杰", nil];
    
    for (NSInteger i = 0; i < 9; i++)
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.backgroundColor = [UIColor colorWithRed:67 / 255.0 green:83 / 255.0 blue:93 / 255.0 alpha:1.0];
        if (i >= 4)
        {
            button.frame = CGRectMake(20 + 70 * (i - 4), 65, 60, 25);
        } else {
            button.frame = CGRectMake(20 + 70 * i, 35, 60, 25);
        }
        [button setTitle:self.arr[i] forState:UIControlStateNormal];
        button.layer.cornerRadius = 10;
        button.tag = 11000 + i;
        button.titleLabel.font = [UIFont systemFontOfSize:16.0];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:button];
    }
    UILabel *labelHis = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 150, 20)];
    labelHis.text = @"历史搜索";
    labelHis.textColor = [UIColor colorWithRed:220 / 255.0 green:220 / 255.0 blue:220 / 255.0 alpha:1.0];
    [view addSubview:labelHis];
    [labelHis release];
    [self getData];
}
- (void)getData
{
    NSString *local = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingString:@"/arr.txt"];
    self.arrLocal = [NSMutableArray arrayWithContentsOfFile:local];
    if (nil == self.arrLocal)
    {
        self.arrLocal = [NSMutableArray array];
    }
    [self.tableView reloadData];
}
- (void)buttonAction:(UIButton *)sender
{
    [self.delegate getButtonTitle:self.arr[sender.tag - 11000]];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate getButtonTitle:self.arrLocal[self.arrLocal.count - 1 - indexPath.row]];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrLocal.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HisTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    cell.delegate = self;
    cell.num = indexPath.row;
    cell.str = self.arrLocal[self.arrLocal.count - 1 - indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
- (void)deleteCell:(NSInteger)num
{
    if (num == self.arrLocal.count)
    {
        num = self.arrLocal.count - 1;
    }
    NSString *local = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingString:@"/arr.txt"];
    [self.arrLocal removeObjectAtIndex:num];
    [self.tableView beginUpdates];
    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForItem:num inSection:0], nil] withRowAnimation:UITableViewRowAnimationRight];
    [self.arrLocal writeToFile:local atomically:YES];
    [self.tableView endUpdates];
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.delegate barResignFirstResponder];
}
@end
